﻿using System;
using System.Linq;

namespace WindowsFormsGUI.Entidades
{
    /// <summary>
    /// [Instruccion] [Peticion] ---> [Respuesta]
    /// </summary>
    public enum PosicionEnum
    {
        Inicio = 0,
        CincoCentavos,
        DiezCentavos,
        VeinticincoCentavos,
        CincuentaCentavos,
        UnPeso,
        Falsa = 100,
        Error = 200
    }

    public static class Utilidades
    {
        public static byte[] HexStringToByteArray(string buffer)
        {
            char[] a;
            int i;
            string temp = string.Empty;

            a = buffer.ToCharArray();

            for (i = 0; i < buffer.Length; i++)
            {
                if (a[i] > 1000)
                    continue;

                temp = temp + Uri.HexEscape(a[i]);
            }

            var hex = temp.Replace("%", "");

            return StringToByteArray(hex);
        }

        private static byte[] StringToByteArray(string hex)
        {
            return Enumerable.Range(0, hex.Length)
                             .Where(x => x % 2 == 0)
                             .Select(x => Convert.ToByte(hex.Substring(x, 2), 16))
                             .ToArray();
        }
    }
}
